<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>批量数据插入最佳实践</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #1e293b;
            border-radius: 12px;
            overflow: hidden;
        }
        .code-header {
            background: #334155;
            padding: 8px 16px;
            font-size: 14px;
            color: #94a3b8;
        }
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
        }
        code {
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 14px;
            line-height: 1.6;
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 8px;
            margin-top: -8px;
            color: #667eea;
        }
        .section-number {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 40px;
            height: 40px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border-radius: 50%;
            font-weight: 700;
            margin-right: 16px;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto">
            <div class="text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6">批量数据插入最佳实践</h1>
                <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto">
                    掌握10万条数据高效插入的核心技术，让你的数据库操作性能提升10倍
                </p>
                <div class="mt-10 flex justify-center space-x-8">
                    <div class="text-center">
                        <i class="fas fa-database text-4xl mb-2"></i>
                        <p class="text-lg">多种数据库支持</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-rocket text-4xl mb-2"></i>
                        <p class="text-lg">性能优化策略</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-code text-4xl mb-2"></i>
                        <p class="text-lg">实战代码示例</p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Introduction -->
    <section class="py-16 px-6">
        <div class="max-w-4xl mx-auto">
            <div class="bg-white rounded-2xl shadow-lg p-10">
                <p class="text-lg leading-relaxed text-gray-700">
                    <span class="drop-cap">批</span>量插入10万条数据时，选择合适的方法和配置可以显著提升插入性能。使用批量插入语句或 API、优化性能设置、处理错误并进行充分测试是确保高效数据插入的关键。本指南将带你深入了解各种数据库的批量插入技术，掌握性能优化的核心要点。
                </p>
            </div>
        </div>
    </section>

    <!-- Process Flow Diagram -->
    <section class="py-16 px-6 bg-gray-100">
        <div class="max-w-6xl mx-auto">
            <h2 class="text-3xl font-bold text-center mb-12 text-gray-800">批量插入流程图</h2>
            <div class="bg-white rounded-2xl shadow-lg p-8">
                <div class="mermaid">
                    graph TD
                        A[开始批量插入] --> B{选择数据存储方案}
                        B --> C[关系型数据库]
                        B --> D[NoSQL数据库]
                        C --> E[准备数据]
                        D --> E
                        E --> F[选择批量插入方法]
                        F --> G[优化性能配置]
                        G --> H[执行批量插入]
                        H --> I{是否有错误?}
                        I -->|是| J[错误处理]
                        J --> H
                        I -->|否| K[验证数据完整性]
                        K --> L[完成]
                        
                        style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                        style L fill:#10b981,stroke:#fff,stroke-width:2px,color:#fff
                        style I fill:#f59e0b,stroke:#fff,stroke-width:2px,color:#fff
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <section class="py-16 px-6">
        <div class="max-w-6xl mx-auto">
            <!-- Section 1 -->
            <div class="mb-16">
                <div class="flex items-center mb-8">
                    <span class="section-number">1</span>
                    <h2 class="text-3xl font-bold text-gray-800">选择合适的数据存储方案</h2>
                </div>
                <div class="grid md:grid-cols-3 gap-6">
                    <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                        <i class="fas fa-database text-3xl text-purple-600 mb-4"></i>
                        <h3 class="text-xl font-semibold mb-3">关系型数据库</h3>
                        <p class="text-gray-600">MySQL、PostgreSQL 等，适合结构化数据，支持事务和复杂查询</p>
                    </div>
                    <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                        <i class="fas fa-leaf text-3xl text-green-600 mb-4"></i>
                        <h3 class="text-xl font-semibold mb-3">NoSQL 数据库</h3>
                        <p class="text-gray-600">MongoDB、Redis 等，适合非结构化数据，高性能读写</p>
                    </div>
                    <div class="bg-white rounded-xl shadow-md p-6 card-hover">
                        <i class="fas fa-cloud text-3xl text-blue-600 mb-4"></i>
                        <h3 class="text-xl font-semibold mb-3">其他存储系统</h3>
                        <p class="text-gray-600">Elasticsearch、ClickHouse 等，适合特定场景的数据存储需求</p>
                    </div>
                </div>
            </div>

            <!-- Section 2 -->
            <div class="mb-16">
                <div class="flex items-center mb-8">
                    <span class="section-number">2</span>
                    <h2 class="text-3xl font-bold text-gray-800">准备数据</h2>
                </div>
                <div class="bg-white rounded-xl shadow-lg p-8">
                    <div class="grid md:grid-cols-2 gap-8">
                        <div>
                            <h3 class="text-xl font-semibold mb-4 text-gray-700">
                                <i class="fas fa-check-circle text-green-500 mr-2"></i>数据准备要点
                            </h3>
                            <ul class="space-y-3 text-gray-600">
                                <li><i class="fas fa-angle-right text-purple-500 mr-2"></i>确保数据格式一致性</li>
                                <li><i class="fas fa-angle-right text-purple-500 mr-2"></i>验证数据完整性和有效性</li>
                                <li><i class="fas fa-angle-right text-purple-500 mr-2"></i>处理特殊字符和编码问题</li>
                                <li><i class="fas fa-angle-right text-purple-500 mr-2"></i>优化数据结构以提高插入效率</li>
                            </ul>
                        </div>
                        <div>
                            <h3 class="text-xl font-semibold mb-4 text-gray-700">
                                <i class="fas fa-file-alt text-blue-500 mr-2"></i>常见数据源
                            </h3>
                            <ul class="space-y-3 text-gray-600">
                                <li><i class="fas fa-angle-right text-purple-500 mr-2"></i>CSV、Excel 文件</li>
                                <li><i class="fas fa-angle-right text-purple-500 mr-2"></i>JSON、XML 格式数据</li>
                                <li><i class="fas fa-angle-right text-purple-500 mr-2"></i>API 接口数据</li>
                                <li><i class="fas fa-angle-right text-purple-500 mr-2"></i>程序生成的模拟数据</li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Section 3 -->
            <div class="mb-16">
                <div class="flex items-center mb-8">
                    <span class="section-number">3</span>
                    <h2 class="text-3xl font-bold text-gray-800">选择合适的批量插入方法</h2>
                </div>
                
                <!-- MySQL Example -->
                <div class="mb-8">
                    <h3 class="text-2xl font-semibold mb-4 text-gray-700">3.1 关系型数据库（MySQL）</h3>
                    
                    <div class="mb-6">
                        <h4 class="text-lg font-semibold mb-3 text-gray-600">使用批量插入语句</h4>
                        <div class="code-block">
                            <div class="code-header">SQL</div>
                            <pre><code class="text-gray-300">INSERT INTO table_name (column1, column2, column3)
VALUES (value1, value2, value3),
       (value4, value5, value6),
       ...
       (valueN1, valueN2, valueN3);</code></pre>
                        </div>
                        <div class="mt-4 bg-blue-50 border-l-4 border-blue-500 p-4 rounded">
                            <p class="text-blue-700"><i class="fas fa-lightbulb mr-2"></i><strong>优点：</strong>减少了数据库与应用程序之间的交互次数，显著提升插入效率。</p>
                        </div>
                    </div>

                    <div class="mb-6">
                        <h4 class="text-lg font-semibold mb-3 text-gray-600">使用 PreparedStatement 批量插入</h4>
                        <div class="code-block">
                            <div class="code-header">Java</div>
                            <pre><code class="text-gray-300">Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "user", "password");
String sql = "INSERT INTO table_name (column1, column2) VALUES (?, ?)";
PreparedStatement pstmt = connection.prepareStatement(sql);

for (int i = 0; i < 100000; i++) {
    pstmt.setString(1, value1);
    pstmt.setString(2, value2);
    pstmt.addBatch();
    
    if (i % 1000 == 0) { // 每1000条执行一次批处理
        pstmt.executeBatch();
    }
}

pstmt.executeBatch(); // 执行剩余的批处理
connection.close();</code></pre>
                        </div>
                    </div>
                </div>

                <!-- MongoDB Example -->
                <div>
                    <h3 class="text-2xl font-semibold mb-4 text-gray-700">3.2 NoSQL 数据库（MongoDB）</h3>
                    
                    <div class="mb-6">
                        <h4 class="text-lg font-semibold mb-3 text-gray-600">使用批量插入 API</h4>
                        <div class="code-block">
                            <div class="code-header">Python</div>
                            <pre><code class="text-gray-300">from pymongo import MongoClient

client = MongoClient('mongodb://localhost:27017/')
db = client['database_name']
collection = db['collection_name']

documents = [{"field1": value1, "field2": value2} for _ in range(100000)]
collection.insert_many(documents)</code></pre>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Section 4 -->
            <div class="mb-16">
                <div class="flex items-center mb-8">
                    <span class="section-number">4</span>
                    <h2 class="text-3xl font-bold text-gray-800">优化性能</h2>
                </div>
                
                <div class="grid md:grid-cols